package com.tfswx.jcyw.ruleenginedesigner.service.impl;

import com.tfswx.jcyw.ruleenginedesigner.bean.ApiResult;
import com.tfswx.jcyw.ruleenginedesigner.dto.fs.FileDownloadInputDTO;
import com.tfswx.jcyw.ruleenginedesigner.service.FsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @author Ricky
 * @date 2021/4/19
 */
@Service
public class FsServiceImpl implements FsService {
    @Override
    public byte[] download(FileDownloadInputDTO inputDto, HttpServletResponse response) throws IOException {

        String filePath = inputDto.getFilePath();
        String fileName = inputDto.getNewFileName();

        if (!StringUtils.hasText(filePath)) {
            return ApiResult.fail("filePath-不能为空").getBytes();
        }

        File file = new File(filePath);
        if (!file.exists()) {
            return ApiResult.fail("文件未找到").getBytes();
        }
        if (!StringUtils.hasText(fileName)) {
            fileName = file.getName();
        }
        return download(file, fileName, response);
    }

    @Override
    public byte[] download(File file, String fileName, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        // 设置强制下载不打开
        response.setContentType("application/force-download");
        response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "utf-8"));
        response.addHeader("fileName", URLEncoder.encode(fileName, "utf-8"));
        response.addHeader("Content-Length", file.length() + "");


        try (InputStream inStream = new FileInputStream(file)) {
            try (OutputStream os = response.getOutputStream()) {
                byte[] buff = new byte[2048];
                int len;
                while ((len = inStream.read(buff)) > 0) {
                    os.write(buff, 0, len);
                }
                os.flush();
            }
        }
        return ApiResult.fail().getBytes();
    }

    @Override
    public byte[] download(byte[] bytes, String fileName, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        // 设置强制下载不打开
        response.setContentType("application/force-download");
        response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "utf-8"));
        response.addHeader("fileName", URLEncoder.encode(fileName, "utf-8"));
        response.addHeader("Content-Length", bytes.length + "");

        try (InputStream inStream = new ByteArrayInputStream(bytes)) {
            try (OutputStream os = response.getOutputStream()) {
                byte[] buff = new byte[2048];
                int len;
                while ((len = inStream.read(buff)) > 0) {
                    os.write(buff, 0, len);
                }
                os.flush();
            }
        }
        return ApiResult.fail().getBytes();
    }
}
